package com.lanxin.oa.personnel.dao;
import com.lanxin.base.BaseDaoImpl;
import com.lanxin.bean.*;
import com.lanxin.util.PageBean;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class EmpDaoImpl extends BaseDaoImpl implements EmpDao {
    @Override
    public PageBean<ZzEmployee> selectEmpPage(Integer page, Integer pageSize, ZzEmployee emp) {
        if (page==null){
            page = 1;
        }
        if(pageSize == null){
            pageSize = 5;
        }
        String hql=("from ZzEmployee z left join fetch z.dept left join fetch z.basic left join fetch z.education where 1=1");
        if(emp.getEmpName()!=null && !"".equals(emp.getEmpName())){

            hql=hql+" and z.empName like '%"+emp.getEmpName()+"%'";
        }
        if(emp.getPhone()!=null && !"".equals(emp.getPhone())){
            hql=hql+" and z.phone like '%"+emp.getPhone()+"%'";
        }
        if(emp.getDeptid()!=null){
            hql=hql+" and z.deptid="+emp.getDeptid();
        }
        if(emp.getJobid()!=null){
            hql=hql+" and z.jobid="+emp.getJobid();
        }
        if(emp.getSex()!=null && !"".equals(emp.getSex())){
            hql=hql+" and z.sex='"+emp.getEmpName()+"'";
        }
        if(emp.getDegree()!=null){
            hql=hql+" and z.degree="+emp.getDegree();
        }
        return super.pageFenYE(hql, page, pageSize);
    }

    @Override
    public List<ZzDepartment> selectDeptName() {

        return queryAll("from ZzDepartment");
    }

    @Override
    public List<JcBasicdatavalue> selectBasic() {
        return queryAll("from JcBasicdatavalue j where j.state=1 and j.type=1");
    }

    @Override
    public List<JcBasicdatavalue> selectBasicEducation() {
        return queryAll("from JcBasicdatavalue j where j.state=1 and j.type=2");
    }

    @Override
    public ZzEmployee selectEmpById(Integer oid) {
        return (ZzEmployee) selectByHQL("from ZzEmployee z left join fetch z.dept left join fetch z.basic left join fetch z.education left join fetch z.account left join fetch z.expriences left join fetch z.jobinfo where z.oid=" + oid);
    }

    @Override
    public ZzAccount selectByUserName(String userName, Integer aid) {
        String hql = "from ZzAccount z where z.username='" + userName + "' ";
        ZzAccount account = null;
        if (aid != null) {
            hql = hql + " and z.aid=" + aid;
            account = (ZzAccount) selectByHQL(hql);
            if (account != null) {
                return null;
            }
            account = (ZzAccount) selectByHQL("from ZzAccount z where z.username='" + userName + "' ");
            if (account == null) {
                return null;
            }
        } else {
            return (ZzAccount) selectByHQL(hql);
        }

        return account;
    }

    @Override
    public List<ZzDepartment> selectDept(Integer oid) {
        return queryAll("from ZzDepartment where manager="+oid);
    }

    @Override
    public List<LcApproval> selectAppro(Integer oid) {
        return queryAll("from LcApproval where status='"+0+"' and approvaluid="+oid);
    }

}
